package www.com.zhongge.application;

import java.util.Stack;

/**
 * @ClassName SequenceMatching
 * @Description TODO 出入栈次序匹配
 * @Author 笨忠
 * @Date 2025/7/24 10:48
 * @Version 1.0
 */
public class SequenceMatching {
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here

        //首先得有一个栈
        Stack<Integer> stack = new Stack<>();
        //遍历pushV
        int j = 0;
        for(int i = 0; i < pushV.length; i++) {
            //入栈
            stack.push(pushV[i]);
            //弹栈：
            while(stack.isEmpty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        //程序走到这里说明pushV遍历完了
        //此时只需要判断栈是否为空，如果是==》则匹配  否则==》不匹配
        if(stack.isEmpty()) {
            return true;
        }
        return false;
    }
}
